package com.hy.mydesktop.client.component.mvc.factory.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.hy.mydesktop.client.MyDesktop;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.mvc.controller.meta.WindowControllerModelEnum;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.client.mvc.desktop.DesktopController;
import com.hy.mydesktop.client.mvc.window.WindowController;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtAppEventType;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentControllerMetaModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentControllerMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.MetaDataConstants;
import com.hy.mydesktop.shared.persistence.domain.util.GxtComponentMetaNodeModelUtil;
import com.hy.mydesktop.shared.persistence.domain.util.GxtComponentMetaNodeModelUtil2;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-10-27；时间：下午02:53:40</li>
 * <li>类型名称：ControllerFactoryUtil</li>
 * <li>设计目的：ControllerFactory生产Controller的辅助类</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class ControllerFactoryUtil {
	
	private static final String className = "com.hy.mydesktop.client.component.mvc.factory.util.ControllerFactoryUtil";
	/**
	 * 
	 * <ul>
	 * <li>方法含义：对窗口的属性进行配置</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-29；时间：下午下午03:56:36</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentControllerMetaNodeModel
	 * @return
	 */
	public static BaseController configureWindow(BaseController baseController,GxtComponentControllerMetaNodeModel gxtComponentControllerMetaNodeModel){
		
		//为Controller，配置对应的控件的gxtComponentMetaNodeModel
		//注：已经与后台匹配过
//		if(gxtComponentControllerMetaNodeModel.get(WindowControllerModelEnum.GXTCOMPONENTMETANODEMODEL.name().toLowerCase())!=null){
		if(gxtComponentControllerMetaNodeModel.get(MetaDataConstants.GXT_COMPONENT__METANODEMODEL)!=null){
			
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel = gxtComponentControllerMetaNodeModel.get(MetaDataConstants.GXT_COMPONENT__METANODEMODEL);
			gxtComponentMetaNodeModel=GxtComponentMetaNodeModelUtil2.reconfigureGxtComponentMetaNodeModelFromJpa(gxtComponentMetaNodeModel);
			baseController.setGxtComponentMetaNodeModel(gxtComponentMetaNodeModel);
		
		}else {
			if (gxtComponentControllerMetaNodeModel.getControllerMetaNodeModelId()==null) {
				
				//MyLoggerUtil.printWarn(className, "gxtComponentControllerMetaNodeModel["+baseController.getClass()+"].getGxtComponentMetaNodeModel() is null");
				MyLoggerUtil.printWarn(MyLoggerUtil.getClassShortName(ControllerFactoryUtil.class),MyLoggerUtil.getClassShortName(baseController.getClass()) +" .getGxtComponentMetaNodeModel() is null (控制器"+MyLoggerUtil.getClassShortName(baseController.getClass())+"没有对应的控件)");
			} else {

				MyLoggerUtil.printWarn(MyLoggerUtil.getClassShortName(ControllerFactoryUtil.class), "gxtComponentControllerMetaNodeModel[Contorller Id :"+gxtComponentControllerMetaNodeModel.getControllerMetaNodeModelId()+"].getGxtComponentMetaNodeModel() is null");
			}
		}
		//为Controller，添加它对应的视图View的标识 ViewData
		if(gxtComponentControllerMetaNodeModel.get(WindowControllerModelEnum.VIEWDATA.name().toLowerCase())!=null){
			
			String viewDataCode = gxtComponentControllerMetaNodeModel.get(WindowControllerModelEnum.VIEWDATA.name().toLowerCase());
			baseController.setViewData(viewDataCode);
		
		}
		//为Controller，添加它对应的视图View的标识ControllerData
		if(gxtComponentControllerMetaNodeModel.get(WindowControllerModelEnum.CONTROLLERDATA.name().toLowerCase())!=null){
			
			String viewDataCode = gxtComponentControllerMetaNodeModel.get(WindowControllerModelEnum.CONTROLLERDATA.name().toLowerCase());
			baseController.setViewData(viewDataCode);
		
		}
		//为Controller添加它的标识 id
		//注：已经与后台匹配过
		
		if(gxtComponentControllerMetaNodeModel.get(MetaDataConstants.CONTROLLER_ID)!=null){
			
			String id = gxtComponentControllerMetaNodeModel.get(MetaDataConstants.CONTROLLER_ID);
			baseController.setId(id);
		
		}else {
			if(gxtComponentControllerMetaNodeModel.get(MetaDataConstants.CONTROLLER_ID.toLowerCase())!=null){
				
				String id = gxtComponentControllerMetaNodeModel.get(MetaDataConstants.CONTROLLER_ID.toLowerCase());
				baseController.setId(id);
			
			}else {
			//if(gxtComponentControllerMetaNodeModel.get(WindowControllerModelEnum.ID.name().toLowerCase())!=null){
				
				String id = gxtComponentControllerMetaNodeModel.get(WindowControllerModelEnum.ID.name().toLowerCase());
				baseController.setId(id);
			
			}
			if (baseController.getId()==null) {
				MyLoggerUtil.printError(className, "没有为当前控件"+baseController.getClass().getName()+"设置控件ID");
			}
		}
		
		//注册BaseController(例如：WindowController)，需要监听的事件
		//if(gxtComponentControllerMetaNodeModel.get(WindowControllerModelEnum.REGISTEREVENTTYPES.name().toLowerCase())!=null){
		if(gxtComponentControllerMetaNodeModel.get(MetaDataConstants.REGISTER_EVENT_TYPES)!=null||gxtComponentControllerMetaNodeModel.get(MetaDataConstants.APP_EVENT_TYPES)!=null){
			
			ControllerFactoryUtilTemplate.addRegisterEventTypeToContorller(baseController,gxtComponentControllerMetaNodeModel);
			
		
		}
		//配置WindowController，需要监听的每类事件，对应的handleEvent方法
		/*if(gxtComponentMetaModel.get(WindowControllerModelEnum.HANDLEEVENT.name().toLowerCase())!=null){
			
			List list = gxtComponentMetaModel.get(WindowControllerModelEnum.HANDLEEVENT.name().toLowerCase());
			
			switch (componentTypeEnum) {
	            case FITLAYOUT_MODEL:
	            		layout=FitLayoutFactory.createFitLayout(gxtComponentMetaModel);
	            		window.setLayout(layout);
	                break;
	            case ACCORDIONLAYOUT_MODEL:
	                	layout=AccordionLayoutFactory.createAccordionLayout(gxtComponentMetaModel);
	                	window.setLayout(layout);
	                break;
           		           
	            default:
	                break;
	        }
			
		}
		if(gxtComponentMetaModel.get(WindowModelEnum.MINWIDTH.name().toLowerCase())!=null){
			//设置窗口的最小宽度
			window.setMinWidth((Integer)gxtComponentMetaModel.get(WindowModelEnum.MINWIDTH.name().toLowerCase()));
		
		}
		
		if(gxtComponentMetaModel.get(WindowModelEnum.LAYOUT.name().toLowerCase())!=null){
			//设置布局方式
			ComponentTypeEnum componentTypeEnum = (ComponentTypeEnum)gxtComponentMetaModel.get(WindowModelEnum.LAYOUT.name().toLowerCase());
	
			Layout layout=null;
	
			switch (componentTypeEnum) {
	            case FITLAYOUT_MODEL:
	            		layout=FitLayoutFactory.createFitLayout(gxtComponentMetaModel);
	            		window.setLayout(layout);
	                break;
	            case ACCORDIONLAYOUT_MODEL:
	                	layout=AccordionLayoutFactory.createAccordionLayout(gxtComponentMetaModel);
	                	window.setLayout(layout);
	                break;
	            case ROWLAYOUT_MODEL:
	                	layout=RowLayoutFactory.createRowLayout(gxtComponentMetaModel);
	                	window.setLayout(layout);
	                break;
	            case VBOXLAYOUT_MODEL:
	                	layout=VboxLayoutFactory.createVboxLayout(gxtComponentMetaModel);
	                	window.setLayout(layout);
	                break;
	            case COLUMNLAYOUT_MODEL:
	                	layout=ColumnLayoutFactory.createColumnLayout(gxtComponentMetaModel);
	                	window.setLayout(layout);
	                break;
	            case FLOWLAYOUT_MODEL:
	                	layout=FlowLayoutFactory.createFlowLayout(gxtComponentMetaModel);
	                	window.setLayout(layout);
	                break;
	            case TABLELAYOUT_MODEL:
	                	layout=TableLayoutFactory.createTableLayout(gxtComponentMetaModel);
	                	window.setLayout(layout);
	                break;
	            case FORMLAYOUT_MODEL:
	                	layout=FormLayoutFactory.createFormLayout(gxtComponentMetaModel);
	                	
	                	window.setLayout(layout);
	                	
	                break;
	            default:
	                break;
	        }
		
		}
		if(gxtComponentMetaModel.get(WindowModelEnum.TOPCOMPONENT.name().toLowerCase())!=null){
			//设置窗口的顶部组建
			window.setTopComponent((Component)gxtComponentMetaModel.get(WindowModelEnum.TOPCOMPONENT.name().toLowerCase()));
		
		}*/
		
		
		
		return baseController;
	}
	
	public static void reConfigureBaseController(BaseController baseController) {
		//对控制器，进行初始化设置（默认情况下，进行初始化设置）
		if (baseController.isInitSelfComponent()) {
			baseController.initialize();
		 }
		baseController.setIinitialized(true);
	}

	public static void renderWindowOfWindowContorller(
			BaseController baseController) {
		if (baseController instanceof WindowController) {
			Desktop desktop = Registry.get(MyDesktop.DESKTOP);
			Window window = ((Window)baseController.getPresenter().getDisplay().getComponent());
		    if (!desktop.getWindows().contains(window)) {
		      desktop.addWindow(window);
				window.show();

		    }
			//((Window)baseController.getPresenter().getDisplay().getComponent()).show();
		}else if (baseController instanceof DesktopController) {
			
		} else {

			MyLoggerUtil.printError(className, "baseController需要为WindowController，实际得到的是"+baseController.getClass().getName());
		}
		
	}

	public static void renderAndSaveWindowOfWindowContorller(GxtComponentControllerMetaModel gxtComponentControllerMetaModel,
			BaseController baseController) {
		
		Map<String, BaseController> gxtControllerMap = Registry.get(MyDesktop.GXTCONTROLLER_MAP);
		if (gxtControllerMap.containsKey(gxtComponentControllerMetaModel.getId())) {
			MyLoggerUtil.printError(className, "gxtControllerMap已经包含了gxtComponentControllerMetaModelId="+gxtComponentControllerMetaModel.getId()+"的键值对");
		} else {
			gxtControllerMap.put(gxtComponentControllerMetaModel.getId(), baseController);
			
			renderWindowOfWindowContorller(baseController);
		}
		/*if (gxtControllerMap.containsKey(baseController.getId())) {
			MyLoggerUtil.printError(className, "gxtControllerMap已经包含了controllerId="+baseController.getId()+"的键值对");
		} else {
			gxtControllerMap.put(baseController.getId(), baseController);
			
			renderWindowOfWindowContorller(baseController);
		}*/
		
		

		
	}
	
	
}
